package com.hzhc.controller.baseinfo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.Machine;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.MachineService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.MachineVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhoumx
 * @date 2024/4/11 16:35
 * @description MachineController
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/machine")
public class MachineController extends BaseController {
    private final MachineService machineService;


/**
 * 机械设备列表
 */
@PostMapping("/getList")
public ResultUtil<List<Machine>> getList(@RequestBody MachineVo vo) {
    Page<Machine> pagination = new Page<Machine>(vo.getPageIndex(), vo.getPageSize());
    List<Machine> datas = machineService.getList(vo, pagination);
    return ResultUtil.build(datas,pagination.getTotal(), pagination.getPages(), ResultUtilCodeEnum.SUCCESS);
}
/**
 * 添加
 */
@PostMapping("/addRet")
@SysTemLog(operModul = "机械设备", operType = "新增", operDesc = "机械设备-新增", modular = "PC")
public ResultUtil<T> addRet(HttpServletRequest request, @RequestBody Machine vo) {
    UserDto user = TokenUtil.getUser(request);
    vo.setUserName(user.getRealname()).setUserId(user.getId());
    return machineService.addRet(vo);
}
/**
 * 修改
 */
@PostMapping("/updateRet")
@SysTemLog(operModul = "机械设备", operType = "修改", operDesc = "机械设备-编辑", modular = "PC")
public ResultUtil<T> updateRet(HttpServletRequest request,@RequestBody Machine vo) {
    return machineService.updateRet(vo);
}
/**
 * 删除
 */
@PostMapping("/delete")
@SysTemLog(operModul = "机械设备", operType = "删除", operDesc = "机械设备-删除", modular = "PC")
public ResultUtil<T> deleteCarNeedPlan(HttpServletRequest request,@RequestBody Machine machine) {
    Machine vo = machineService.getById(machine.getId());
    return machineService.updateDelete(vo);
}
/**
 * 校验设备名称
 */
@RequestMapping("/checkMachineName")
public ResultUtil<T> checkMachineName(@RequestBody Machine machine){
    String field = "machine_name";
    Machine m =machineService.checkMachine(field, machine.getMachineName(), machine.getId());
    if (m!=null){
        return ResultUtil.build(ResultUtilCodeEnum.MACHINE_NAME_EXIST);
    }else{
        return ResultUtil.ok();
    }
}
/**
 * 校验设备编号
 */
@RequestMapping("/checkMachineNo")
public ResultUtil<T> checkMachineNo(@RequestBody Machine machine){
    String field="machine_no";
    Machine m=machineService.checkMachine(field,machine.getMachineName(), machine.getId());
    if (m!=null){
        return ResultUtil.build(ResultUtilCodeEnum.MACHINE_NO_EXIST);
    }else{
        return ResultUtil.ok();
    }
}
}
